﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class VideoPageFuncitionDAL
    {
        /// <summary>
        /// 通过前端传过来的用户信息表id和视频信息表id给收藏表添加数据
        /// </summary>
        /// <returns></returns>
        public Response_DTO<string> AddtoCollection(AddCollection_DTO mod)
        {
            Response_DTO<string> res = new Response_DTO<string>();

            // 参数验证
            if (mod.UserInfoId <= 0)
            {
                res.Success = false;
                res.Message = "用户信息错误";
                res.Conten = "NO";
                return res;
            }

            if (mod.VideoInfoId <= 0)
            {
                res.Success = false;
                res.Message = "视频信息错误";
                res.Conten = "NO";
                return res;
            }

            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                // 一次性查询所有需要的验证信息
                var userExists = con.UserInfos.Any(u => u.Id == mod.UserInfoId);
                var videoExists = con.VideoInfos.Any(v => v.Id == mod.VideoInfoId);
                var alreadyCollected = con.Collections.Any(c => c.UserInfoId == mod.UserInfoId && c.VideoInfoId == mod.VideoInfoId);

                // 检查用户是否存在
                if (!userExists)
                {
                    res.Success = false;
                    res.Message = "用户不存在";
                    res.Conten = "NO";
                    return res;
                }

                // 检查视频是否存在
                if (!videoExists)
                {
                    res.Success = false;
                    res.Message = "视频不存在";
                    res.Conten = "NO";
                    return res;
                }

                // 检查是否已经收藏过该视频
                if (alreadyCollected)
                {
                    res.Success = false;
                    res.Message = "您已经收藏过该视频了";
                    res.Conten = "NO";
                    return res;
                }

                // 执行收藏操作
                try
                {
                    Collection collection = new Collection()
                    {
                        UserInfoId = mod.UserInfoId,
                        VideoInfoId = mod.VideoInfoId
                    };

                    con.Collections.Add(collection);
                    int count = con.SaveChanges();

                    if (count > 0)
                    {
                        res.Success = true;
                        res.Message = "收藏成功";
                        res.Conten = "YES";
                    }
                    else
                    {
                        res.Success = false;
                        res.Message = "收藏失败";
                        res.Conten = "NO";
                    }
                }
                catch (Exception ex)
                {
                    res.Success = false;
                    res.Message = "收藏失败：" + ex.Message;
                    res.Conten = "NO";
                }
            }

            return res;
        }
    }
}
